<?php

/**
 * RewardLogModel 模型
 * Class RewardLogModel
 */
class RewardLogModel extends BaseModel
{

    public $tableName = "reward_log";
    public $errMsg = '';

    static private $_singleton;

    public function __construct()
    {
        parent::__construct($this->tableName);
    }

    static public function getSingleton()
    {
        if (!self::$_singleton) {
            self::$_singleton = new static();
        }

        return self::$_singleton;
    }

    static $typeText = [
        1   => '直营奖励',
        2   => '网店身份奖励',
        3   => '实体店身份奖励'
    ];

    public function add($data)
    {
        try {
            $selfModel = static::getSingleton();
            return $selfModel->insert([
                'type'          => $data['type'],
                'user_id'       => $data['user_id'],
                'from_user_id'  => $data['from_user_id'],
                'from_user_type'    => $data['from_user_type'],
                'order_id'      => $data['order_id'],
                'point'         => $data['point'],
                'point_base'    => $data['point_base'],
                'money'         => $data['money'],
                'ratio'         => $data['ratio'],
                'status'        => $data['status'],
                'self_ratio'    => isset($data['self_ratio'])? $data['self_ratio']: '',
                'o_ratio'       => isset($data['o_ratio'])? $data['o_ratio']: '',
                'o_user_id'     => isset($data['o_user_id'])? $data['o_user_id']: '',
                'description'   => isset($data['description'])? $data['description']: '',
                'create_time'   => time()
            ]);
        } catch (Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    //计算月度直营业绩
    public function getMonthSaleMoneyTotal( $month_date, $user_id ){
        //获取月开始和结束时间
        $monthArr = $this->getthemonth($month_date);
        $moneyTotal = self::getSingleton()->find([
            'create_time#>='=> $monthArr['date_s'],
            'create_time#<='=> $monthArr['date_e'],
            'type' => 1,
            'user_id'   => $user_id
            ], 'sum(money) as moneyTotal');
        return $moneyTotal['moneyTotal'];
    }

    //月度直营业绩列表
    public function getMonthSaleMoneyList($month_date, $user_id){
        $monthArr = $this->getthemonth($month_date);
        $moneyList = self::getSingleton()->findAll([
            'create_time#>='=> $monthArr['date_s'],
            'create_time#<='=> $monthArr['date_e'],
            'type' => 1,
            'user_id'   => $user_id
        ]);
        return $moneyList;
    }

    //获取年度直营业绩
    public function getYearSaleMoneyTotal($month_date, $user_id) {
        //年度开始时间
        $data_s = $this->yearStartTime();
        $monthArr = $this->getthemonth($month_date);
        $moneyTotal = self::getSingleton()->find([
            'create_time#>='=> $data_s,
            'create_time#<='=> $monthArr['date_e'],
            'user_id'   => $user_id,
            'type' => 1,
        ], 'sum(money) as moneyTotal');
        return $moneyTotal['moneyTotal'];
    }

    //月度团队业绩
    public function getMonthGroupMoneyTotal($month_date, $user_id){
        //旗下所有店铺
        $userList = UserModel::getSingleton()->findAll(['refer_user_id'=> $user_id], 'user_id');
        $groupMoneyTotal = 0;
        foreach ($userList as &$val){
            //当前店铺的直营业绩就是上级推荐人的团队业绩
            $val['sales_money_total'] = $this->getMonthSaleMoneyTotal($month_date,$val['user_id']);
            $groupMoneyTotal += $val['sales_money_total'];
        }
        return $groupMoneyTotal;
    }

    //年度团队业绩
    public function getYearGroupMoneyTotal($month_date, $user_id){
        //旗下所有店铺
        $userList = UserModel::getSingleton()->findAll(['refer_user_id'=> $user_id], 'user_id');
        $groupMoneyTotal = 0;
        foreach ($userList as &$val){
            //当前店铺的直营业绩就是上级推荐人的团队业绩
            $val['sales_money_total'] = $this->getYearSaleMoneyTotal($month_date, $val['user_id']);
            $groupMoneyTotal += $val['sales_money_total'];
        }
        return $groupMoneyTotal;
    }

    //获取月度业绩提成比例
    public function getMonthRatio($month_date, $user_id){
        //上月时间范围
        $prevMonth = $this->getprevmonth($month_date);
        //上月直营业绩
        $prevMonthMoneyTotal = $this->getMonthSaleMoneyTotal($prevMonth, $user_id);
        //上月团队业绩
//        $prevMonthGroupMoneyTotal = $this->getMonthGroupMoneyTotal( $prevMonth, $user_id );
        //上月总业绩
//        $prevMonthMoneyAll = $prevMonthMoneyTotal + $prevMonthGroupMoneyTotal;
        //匹配月度比例
        $monthRatio = $this->monthRatio($prevMonthMoneyTotal);

        return $monthRatio;
    }

    //获取年度业绩提成比例
    public function getYearRatio($month_date, $user_id){
        //年度直营业绩
        $yearSaleMoneyTotal = $this->getYearSaleMoneyTotal($month_date ,$user_id);
        //年度团队业绩
//        $yearGroupMoneyTotal = $this->getYearGroupMoneyTotal($month_date,$user_id);
        //年度业绩
//        $yearMoneyAll = $yearSaleMoneyTotal + $yearGroupMoneyTotal;

        //匹配比例
        $yearRatio = $this->yearRatio($yearSaleMoneyTotal);

        return $yearRatio;
    }

    //用户总终提成比例
    public function getRatio($month_date, $user_id){
        //月度提成比例
        $monthRatio = $this->getMonthRatio($month_date, $user_id);
        //年度提成比例
        $yearRatio = $this->getYearRatio($month_date, $user_id);

        return $monthRatio + $yearRatio;
    }

    //月度获得的总积分
    public function getMonthPointTotal($month_date, $user_id){
        //上月时间范围
        $monthArr = $this->getthemonth($month_date);
        //上月获得的积分
        $pointDAta = self::getSingleton()->find([
            'create_time#>='=> $monthArr['date_s'],
            'create_time#<='=> $monthArr['date_e'],
            'user_id'   => $user_id
        ], 'sum(point) as point_total');
        return $pointDAta['point_total'];
    }

    //指定月份开始结束时间
    public function getthemonth($date)
    {
        $firstday = date('Y-m-01', $date).' 00:00:00';
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day")).' 00:00:00';
        return array('date_s'=> strtotime($firstday),'date_e'=> strtotime($lastday));
    }

    //指定月份上月时间
    public function getprevmonth($date){
        $firstday = date('Y-m-d', $date);
        $prevMonth = date('Y-m-d', strtotime("$firstday -1 month"));
        return strtotime($prevMonth);
    }

    //本年度开始时间
    public function yearStartTime(){
        return strtotime(date('Y-01-01 00:00:00', time()));
    }

    //月份业绩提成配置
    public function monthRatio($kpi){
        $ratio = 0.1;
        if($kpi>=10000 && $kpi<20000){
            $ratio = 0.2;
        }
        if($kpi>=20000 && $kpi<30000){
            $ratio = 0.3;
        }
        if($kpi>=30000){
            $ratio = 0.4;
        }
        return $ratio;
    }

    //年度业绩提成配置
    public function yearRatio($kpi){
        $ratio = 0.1;
        if($kpi>=20000 && $kpi<30000){
            $ratio = 0.2;
        }
        if($kpi>=30000 && $kpi<50000){
            $ratio = 0.3;
        }
        if($kpi>=50000){
            $ratio = 0.4;
        }
        return $ratio;
    }

}